package com.demo.druid.modules.system.user.service.impl;

import com.demo.druid.component.datasource.annotation.DataSource;
import com.demo.druid.component.datasource.annotation.DataSourceKey;
import com.demo.druid.component.util.PageHelperUtils;
import com.demo.druid.modules.system.user.dao.UserMapper;
import com.demo.druid.modules.system.user.entity.User;
import com.demo.druid.modules.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author wangjianhua
 * @Description
 * @date 2021/9/1/001 14:00
 **/
@Service
//@Transactional(readOnly = true, rollbackFor = Exception.class)
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询
     * @param
     * @return 分页结果
     */
    @Override
    public List<User> find(User user){

        PageHelperUtils.startPage(user.getPage(),user.getLimit());
        return userMapper.getByPage();


    }

    /**
     * 用户新增
     * @param entity 用户实体类
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int save(User entity){

        entity.setCreatedTime(new Date());
        entity.setUpdatedTime(new Date());
        return userMapper.save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void update(User user) {
        userMapper.update(user);
    }

    /**
     * 根据主键id删除用户
     * @param id id
     */
    @Override
    @Transactional(readOnly = false)
    public void deleteById(int id){
        userMapper.deleteById(id);
    }

    /**
     * 根据主键获取用户
     * @param id 主键
     * @return 用户
     */
    @Override
    public User findById(int id){
        userMapper.getByPage();
        return userMapper.getById(id);
    }
}
